﻿using EntityBinder.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using EntityBinder;
using EntityBinder.Attribute;
using EntityBinder.Binder;
using EntityBinder.Transformer;
using EntityBinder.Json;

namespace TestApp
{
	class Program
	{
		static void Main(string[] args)
		{
			TestXmlReader();

			TestJson();



			Console.WriteLine("----- Done -----");
			Console.ReadLine();
		}

		static void TestJson()
		{
			Console.WriteLine("Reading...");
			/*
			using(FileStream file = File.OpenRead("./test.json"))
			{
				IEntityReader reader = EntityReaderFactory.GetReader<ObjectContainer>(EntityBinderFormat.JSON);

				ObjectContainer data = reader.Read<ObjectContainer>(file);

				Console.WriteLine("Data = ");
				Console.WriteLine(data.ToString());
			}
			 */
			Console.WriteLine("Writing...");

			if(File.Exists("./simple-out.json"))
			{
				File.Delete("./simple-out.json");
			}
			using(FileStream file = File.OpenWrite("./simple-out.json"))
			{
				IEntityWriter writer = EntityWriterFactory.GetWriter<ObjectContainer>(EntityBinderFormat.JSON);
				writer.Indent = true;
				ObjectContainer obj = new ObjectContainer()
				{
					ID = "myide",
					Container = new DataContainer("hello", "world", 42, 31)
				};
				obj.Strings.Add("az");
				obj.Strings.Add("za");
				obj.Datas.Add(new DataContainer("azerty", "qsdfg", 51, 69));
				obj.Datas.Add(new DataContainer("a2s1", "a2s2", 1, 2));


				writer.Write(file, obj);
			}
		}
		

		static void TestXmlReader()
		{
			ObjectContainer dataObject;

			IEntityReader reader = EntityReaderFactory.GetReader<ObjectContainer>(EntityBinderFormat.XML);
			using(FileStream file = File.Open(@"./testInput.xml", FileMode.Open, FileAccess.Read))
			{
				dataObject = reader.Read<ObjectContainer>(file);

				Console.WriteLine(dataObject.ToString());
			}

			IEntityWriter writer = EntityWriterFactory.GetWriter<ObjectContainer>(EntityBinderFormat.XML);
			writer.Indent = true;
			string outputFile = @"./testOutput.xml";
			if(File.Exists(outputFile))
			{
				File.Delete(outputFile);
			}
			using(FileStream file = File.Open(outputFile, FileMode.Create, FileAccess.Write))
			{
				writer.Write(file, dataObject);
			}
		}
	}

	[Entity(Identifier="data")]
	class DataContainer
	{
		[DataEntity(Identifier="s1")]
		public string stringPublic;

		[DataEntity(Identifier="s2")]
		private string m_stringPrivate;

		[DataEntity(Identifier="n1")]
		public int intProperty
		{
			get;set;
		}

		[DataEntity(Identifier="n2")]
		private int intPrivate
		{
			get;set;
		}

		public DataContainer()
		{
			stringPublic = null;
			m_stringPrivate = null;

			intPrivate = -1000;
			intProperty = -1000;
		}

		public DataContainer(string _s1, string _s2, int _n1, int _n2)
		{
			stringPublic = _s1;
			m_stringPrivate = _s2;
			intProperty = _n1;
			intPrivate = _n2;
		}

		public override string ToString()
		{
			StringBuilder builder = new StringBuilder();

			builder.AppendFormat("S1 = {0} S2 = {1} N1 = {2} N2 = {3}", stringPublic, m_stringPrivate, intProperty, intPrivate);

			return builder.ToString();
		}
	}

	[Entity(Identifier="root")]
	class ObjectContainer
	{
		[DataEntity(Identifier="ID")]
		public string ID { get; set; }

		[DataEntity(Identifier="Container")]
		public DataContainer Container { get; set; }

		[DataEntity(Identifier="string", ContainerIdentifier="strings")]
		public List<string> Strings = new List<string>();

		[DataEntity(Identifier="dataObject", ContainerIdentifier="datas")]
		public List<DataContainer> Datas = new List<DataContainer>();

		public ObjectContainer()
		{
			
		}

		public override string ToString()
		{
			StringBuilder builder = new StringBuilder();

			builder.AppendFormat("ID = {0}\nContainer = [{1}]\n", ID, Container.ToString());

			builder.Append("Strings : [");
			foreach(string st in Strings)
			{
				builder.Append(st);
				builder.Append(", ");
			}
			if(Strings.Count > 0)
				builder.Remove(builder.Length - 2, 2);
			builder.AppendLine("]");

			builder.AppendLine("Datas : [");
			foreach(DataContainer dc in Datas)
			{
				builder.AppendLine("\t\t[ " + dc.ToString() + " ]");
			}
			builder.AppendLine("        ]");

			return builder.ToString();
		}
	}
}
